﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.Globalization;
using BlueLotus7.Eden.Resource;

namespace BlueLotus7.Eden
{
    /// <summary>
    /// Resource Helper
    /// </summary>
    public static class ResourceHelper
    {
        #region Private fields

        private static ResourceManager enUS = EdenResource_enUS.ResourceManager;
        private static ResourceManager zhCN = EdenResource_zhCN.ResourceManager;

        #endregion

        #region Public methods

        public static string GetStringByKey(string key, string cultureName)
        {
            string result = string.Empty;

            ResourceManager manager = GetResourceManager(cultureName);
            result = manager.GetString(key);

            return result;
        }

        public static string GetStringByKey(string key, CultureInfo cultureInfo)
        {
            string result = string.Empty;

            ResourceManager manager = GetResourceManager(cultureInfo);
            result = manager.GetString(key);

            return result;
        }        

        #endregion

        #region Private methods

        private static ResourceManager GetResourceManager(string cultureName)
        {
            ResourceManager resourceManager = null;

            if (!string.IsNullOrWhiteSpace(cultureName))
            {
                switch (cultureName.Replace("-", string.Empty).ToLowerInvariant())
                {
                    case "zh":
                    case "zhcn":
                    case "cn":
                        resourceManager = zhCN;
                        break;
                    case "en":
                    case "enus":
                    default:
                        resourceManager = enUS;
                        break;
                }
            }

            if (resourceManager == null)
            {
                resourceManager = enUS;
            }

            return resourceManager;
        }

        private static ResourceManager GetResourceManager(CultureInfo cultureInfo)
        {
            string cultureName = string.Empty;

            if (cultureInfo != null)
            {
                cultureName = cultureInfo.Name;
            }

            return GetResourceManager(cultureName);
        }

        #endregion
    }
}
